/**
 * Copyright 2005 The Apache Software Foundation
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.edi.runtime.component
{
	import com.edi.runtime.Event.*;
	
	import flash.events.MouseEvent;
	
	import mx.containers.Canvas;
	import mx.controls.RadioButton;
	import mx.events.FlexEvent;
	
	/**
	 * 创建RadioButton组.继承自 Canvas ，并实现了IEDIComponent，IEDIDataLoad 借口 
	 * 
	 */		
	public class EDIRadioButtonGroup extends Canvas  implements IEDIComponent,IEDIDataLoad
	{
		private var _items:Object;
		private var _field:String;
		private var _table:String;
		private var _primaryKey:String="";
		private var _fieldtype:String;
		private var _fieldLength:int;	
		private var _dataSourceName:String="";	
		private var _labelField:String="@label";
		private var _dataField:String="@value";
		
		[Bindable] 
		/**
		 * 被选择的值 
		 */		
		public var selectItemValue:String;		  
	
		
		/**
		 *  构造EDIRadioButtonGroup
		 *  <component:EDIRadioButtonGroup field="FDateField1"  fieldType="文本"  y="111"  x="103"  height="49"  fieldLength="50"  visible="true"  primaryKey="false"  id="radioBtnList"  width="122" ><component:items><mx:XMLList xmlns="">
		 * 	 <item key="内部新闻" value="nbxw" />
		 * 	 <item key="每日一贴" value="mryt" /></mx:XMLList></component:items>
		 *  </component:EDIRadioButtonGroup>
		 */			
		public function EDIRadioButtonGroup()
		{
			super();
		
			addEventListener(FlexEvent.CREATION_COMPLETE,creationComplete);
		}
		
		/**
		 *实现IEDIComponent接口的属性，设置表名。 
		 * @param value
		 * 
		 */		
		public function set table(value:String):void
		{
			_table=value;
			EDIEventProxy.listenerDataRolling(value,this);
		}
		public function get table():String
		{return _table;}
		
		/**
		 * 显示用字段
		 * @param value
		 * 
		 */				
		public function set labelField(value:String):void
		{_labelField=value;}
		public function get labelField():String
		{return _labelField;}
		
		/**
		 * 数据字段，用户日后取出真是数据所用 
		 * @param value
		 * 
		 */		
		public function set dataField(value:String):void
		{_dataField=value;}
		public function get dataField():String
		{return _dataField;}		
		
		/**
		 * 数据源名称 
		 * @param value
		 * 
		 */		
		public function set dataSourceName(value:String):void
		{
			_dataSourceName=value;
			EDIEventProxy.listenerDataLoad(value,this);	
		}
		public function get dataSourceName():String
		{return _dataSourceName;}
		
		/**
		 * 实现IEDIComponent接口的属性，设置是否是主键 
		 * @param value
		 * 
		 */		
		public function set primaryKey(value:String):void
		{_primaryKey=value;}
		public function get primaryKey():String
		{return _primaryKey;}
		
		/**
		 * 实现IEDIComponent接口的属性，设置字段名 
		 * @param value
		 * 
		 */		
		public function set field(value:String):void
		{
			_field=value;
		}
		
		public function get field():String
		{
			return _field;
		}
		
		/**
		 * 实现IEDIComponent接口的属性，设置字段类型 
		 * @param value
		 * 
		 */		
		public function set fieldType(value:String):void{_fieldtype=value;}
		public function get fieldType():String{return _fieldtype;}
		
		/**
		 * 实现IEDIComponent接口的属性，设置字段长度 
		 * @param value
		 * 
		 */		
		public function set fieldLength(value:int):void{_fieldLength=value;}
		public function get fieldLength():int{return _fieldLength;}
		
		/**
		 * 列表数据集 
		 * @param value
		 * 
		 */		
		public function set items(value:Object):void
		{
			
			_items=value;
			//var compValue:RadioButton
			if(value!=null && value is XMLList)
			{
				var i:int;
				var compValue:RadioButton
				for(i=0;i<(value as XMLList).length();i++)
				{
					compValue=new RadioButton();
					compValue.label=value[i]["@key"].toString();
					compValue.value=value[i]["@value"].toString();
					compValue.x=0;
					compValue.y=i*20;
					compValue.width=100;
					compValue.height=20;
					compValue.addEventListener(MouseEvent.CLICK,rbclickHandle);
					compValue.groupName=field;
					this.addChild(compValue);
				}
			}

		}
		
		/**
		 * 选项集合 
		 * @return 
		 * 
		 */		
		public function itemArray():Array
		{
			var ret:Array=new Array();
			var arr:Array=this.getChildren();
			for(var i:int=0;i<arr.length;i++)
			{
				if(arr[i] is RadioButton)
				{
					ret.push(arr[i]);
				}
			}
			return ret;
		}
		
		
		private function creationComplete(event:FlexEvent):void
		{
			if(_items!=null && _items is XMLList)
			{
				this.height=(_items as XMLList).length()*20;
			}
		}
		
		/**
		 * 实现IEDIDataLoad接口的属性，加载数据集 
		 * @param event
		 * 
		 */		
		public function dataLoadHandler(event:EDIDataEvent):void
		{
			items=event.value;
		}
		
		/**
		 * 实现IEDIComponent接口的属性，捕获数据滚动 
		 * @param event
		 * 
		 */		
		public function dataRollingHandler(event:EDIDataEvent):void
		{
			if(_items!=null)
			{
				var i:int;
				for(i=0;i<_items.length;i++)
				{
					if(_items[i][dataField].toString()==event.value[field].toString())
					{
						(getChildren()[i] as RadioButton).selected=true;
						selectItemValue=(getChildren()[i] as RadioButton).value.toString();
						break;					
					}
				}
			}
		}
		
		private function rbclickHandle(event:MouseEvent):void
		{
			this.selectItemValue=(event.target as RadioButton).value.toString();
		}
		
		/**
		 * 返回数据监听时所需要的值 
		 * @return 
		 * 
		 */		
		public function get ediValue():String
		{
			return selectItemValue;
		}
		
		
		public function set selectRadioForValue(value:String):void
		{
			var arr:Array=this.getChildren();
			for(var i:int=0;i<arr.length;i++)
			{
				if(arr[i] is RadioButton)
				{
					if((arr[i] as RadioButton).value==value)
					{
						(arr[i] as RadioButton).selected=true;
						break;
					}
				}
			}
		}
		
	}
}